﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAO
{
    public class ThongTinTaiKhoanDAO
    {
        private FDeal4Entities db;

        public ThongTinTaiKhoanDAO()
        {
            db = new FDeal4Entities();
        }

        public List<THONGTINTAIKHOAN> LietKe()
        {
            return db.THONGTINTAIKHOAN.ToList();
        }

        public THONGTINTAIKHOAN LayID(string id)
        {
            var tl = db.THONGTINTAIKHOAN.SingleOrDefault(t => t.IDTAIKHOAN == id);
            return tl;
        }
        public THONGTINTAIKHOAN Layusername(string user)
        {
            var tl = db.THONGTINTAIKHOAN.SingleOrDefault(t => t.TENDANGNHAP == user);
            return tl;
        }
        public THONGTINTAIKHOAN TimTenDangNhap(String keyWord)
        {
            var tl = db.THONGTINTAIKHOAN.SingleOrDefault(t => t.TENDANGNHAP.Contains(keyWord));
            return tl;
        }
        //public THONGTINTAIKHOAN TimMatKhau(String id)
        //{
        //    var tl = db.THONGTINTAIKHOAN.SingleOrDefault(t => t.MATKHAU.Contains(keyWord));
        //    return tl;
        //}


        public bool Them(THONGTINTAIKHOAN tl)
        {
            try
            {
                db.THONGTINTAIKHOAN.AddObject(tl);
                db.SaveChanges();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }
        public bool KiemTraName(string user)
        {
            var tl = db.THONGTINTAIKHOAN.SingleOrDefault(t => t.TENDANGNHAP == user);
            if (tl != null)
                return true;
            return false;
        }
        public bool Xoa(THONGTINTAIKHOAN tl)
        {
            try
            {
                db.THONGTINTAIKHOAN.DeleteObject(tl);
                db.SaveChanges();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

        public bool Sua(THONGTINTAIKHOAN tl)
        {
            try
            {
                THONGTINTAIKHOAN t1 = db.THONGTINTAIKHOAN.Single(t => t.IDTAIKHOAN == tl.IDTAIKHOAN);
                t1.IDTAIKHOAN = tl.IDTAIKHOAN;
                t1.TENDANGNHAP = tl.TENDANGNHAP;
                t1.EMAIL = tl.EMAIL;
                t1.MATKHAU = tl.MATKHAU;
                t1.QUOCGIA = tl.QUOCGIA;
                t1.TINHTHANH = tl.TINHTHANH;
                t1.GIOITINH = tl.GIOITINH;
                t1.NGAYSINH = tl.NGAYSINH;
                t1.CAUHOIBIMAT = tl.CAUHOIBIMAT;
                t1.TRALOICHBM = tl.TRALOICHBM;
                t1.LOAITAIKHOAN = tl.LOAITAIKHOAN;

                db.SaveChanges();
                return true;
            }
            catch (System.Exception ex)
            {
            	return false;
            }
        }
    }
}
